package com.base.cn.platform.os.controller.qa.record;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.qa.record.QaGainAttendRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 问题旁听收益记录Controller
 *
 * @author LIZHIPENG
 * @since 2018/5/2
 */
@Controller
@RequestMapping("/admin/qa/record")
public class AdminQaGainAttendRecordController extends AdminBaseController {

    @Autowired
    private QaGainAttendRecordService attendRecordService;

    /**
     * 查询旁听分成收益数量
     * @param request HttpServletRequest
     * @return Map<String,Object>
     */
    @RequestMapping("/getGainAttenRecordCount")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "提现旁听分成收益")
    public Map<String,Object> getGainAttenRecordCount(HttpServletRequest request){
        BigDecimal teacherId = loginUtil.getLoginUserTeacherId(request);
        if(BigDecimal.ZERO.compareTo(teacherId)==0){
            teacherId = new BigDecimal(-1);
        }
        Map<String,Object> params = new HashMap<>();
        params.put("receiptorId",teacherId);
        params.put("status",1);
        params.put("userType",2);
        BigDecimal count = attendRecordService.getGainAttenRecordCount(params);
        if(count==null || count.compareTo(BigDecimal.ZERO)==0){
            return ResultUtil.ERROR("当前登录账号没有可提现的旁听分成记录");
        }
        return ResultUtil.SUCCESS(count);
    }

    /**
     * 初始化旁听收益记录列表页面
     * @return
     */
    @RequestMapping("/queryGainAttendList")
    public String queryGainAttendList(Model model,@RequestParam(value = "type", required = false, defaultValue = "2")int type,@RequestParam(value = "questionId", required = false)BigDecimal questionId){
        model.addAttribute("type",type);
        model.addAttribute("questionId",questionId);
        return resultPage("/qa/record/qa-gain-attend-record-list");
    }

    /**
     * ajax加载旁听收益详细记录
     * @param model
     * @param params
     * @return
     */
    @RequestMapping("/ajaxQueryGainAttendList")
    public String ajaxQueryGainAttendList(HttpServletRequest request,
                                          Model model,
                                          @RequestParam(value = "currentPage", required = false, defaultValue = "1")int currentPage,
                                          @RequestBody Map<String, Object> params){
        BigDecimal teacherId = loginUtil.getLoginUserTeacherId(request);
        if(DataUtil.idIsNotNull(teacherId)){
            params.put("receiptorId",teacherId);
            params.put("userType",2);
        }
        Map<String, Object> dataMap =null;
        Integer type =  Integer.valueOf(params.get("type").toString());
        params.remove("type");
        if(type==2){
            dataMap = attendRecordService.queryAdminGainAttendRecordListPage(currentPage, params);
        }else{
            dataMap = attendRecordService.queryGainAttendRecordListPage(currentPage, params, true, true);

        }
        model.addAttribute("type", type);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/qa/record/ajax-qa-gain-attend-record-list");
    }
}
